/**
 * 功能描述
 */
define(function (require, exports, module) {
	var Grid = require("grid");
	var aw = require("ajaxwrapper");
	var store =require("store");
	var activeUser = store.get("user");
	var roles = activeUser.roles;
	var datilCarGrid = require("../component/datilCarGrid");
	var ciiCarGrid = require("../component/ciiCarGrid");
    var instance = {
        initComponent: function (widget) {
        	var grid = new Grid({
        		parentNode : ".J-grid",
        		autoRender: false,
        		model : {
        			url : "api/checkinimplement/queryallcheckinorder",
            		params : function(){
            			var subnav = widget.get("subnav");
            			return {
            				pkServicePoint: subnav.getValue("servicePoint"),
            				"status": subnav.getValue("status"), 
            				startDate : subnav.getValue("startDate").start,
            				endDate : subnav.getValue("startDate").end,
            				fetchProperties:"pkCIImplement,carDispatchBill.carModel,carDispatchBill.*," +
							"carDispatchBill.companyName,carDispatchBill.unitsCar,carDispatchBill.driverName,carDispatchBill.driverPhone," +
							"carDispatchBill.car.carType,carDispatchBill.car.carNumber," +
							"carDispatchBill.driver.personalInfo.name," +
							"carDispatchBill.driver.personalInfo.phone," +
							"version,roomStatus.*,status.*," +
							"dataSource.*,signUpNum," + 
							"nonMemGroupReserve.*,nonMemGroupReserve.nonMemberGroup," + 
							"nonMemGroupReserve.nonMemberGroup.servicePoint.name," + 
							"nonMemGroupReserve.nonMemberGroup.*," + 
							"nonMemGroupReserve.signUpRooms.*," + 
							"nonMemGroupReserve.signUpRooms.roomType.*," + 
							"nonMemGroupReserve.nonMemberGroup.signUps.version," + 
							"nonMemGroupReserve.nonMemberGroup.operator.servicePoint.name," + 
							"groupReserve.planItemGroup.allianceOrg.name," +
							"groupReserve.signUpNumber," +
							"groupReserve.checkInDate," +
							"groupReserve.checkOutDate," +
							"groupReserve.planItemGroup.planItem.destination.name," +
							"groupReserve.planItemGroup.signUps.version," +
							"groupReserve.planItemGroup.signUps.pkSignUpRegister," +
							"groupReserve.roomRegisters.pkRoomRegister," +
							"groupReserve.roomRegisters.room.code," +
							"groupReserve.roomRegisters.room.orgBuilding.name," +
							"groupReserve.planItemGroup.servicePoint.pkServicePoint," +
							"groupReserve.planItemGroup.servicePoint.name," +
							"checkInCar," + 
							"checkInCar.pkCheckInCar," + 
							"checkInCar.version," +
							"checkInCar.haveCar," +
							"checkInCar.address," + 
							"checkInCar.status," +
							"checkInCar.description," +
							"checkInCar.driverName," +
							"checkInCar.driverPhone," +
							"checkInCar.corpName," +
							"checkInCar.carDate," +
							"checkInDinner.pkCheckInDinner," +
							"checkInDinner.version," + 
							"checkInDinner.haveDinner," +
							"checkInDinner.breakfast," +
							"checkInDinner.lunch," +
							"checkInDinner.supper," +
							"checkInDinner.status," +
							"checkInDinner.description," +
							"checkInMeetings.pkCheckInMeeting," +
							"checkInMeetings.haveMeeting," +
							"checkInMeetings.screenings," +
							"checkInMeetings.meetingDate," +
							"checkInMeetings.screenings," +
							"checkInMeetings.meetingRoom," +
							"checkInMeetings.peoCount," +
							"checkInMeetings.meetPlaceType," +
							"checkInMeetings.content," +
							"checkInMeetings.contentLED," +
							"checkInMeetings.license," +
							"checkInMeetings.description," +
							"checkInMeetings.status," +
							"checkInMeetings.version," +
							"descript," +
							"signUpRoom.pkSignUpRoom," +
							"signUpRoom,version," +
							"signUpRoom.descript"
            			}
            		},
        			columns : [{
        				name : "group",
        				label : "团/散客",
        				format : function(value, row){
        					if(row.dataSource.key == "GroupReserve"){
        						return "老人团";
        					}else{
        						return "散客";
        					}
        				}
        			},{
        				name:"groupReserve",
        				label:"组团单位/预约人",
        				format: function(value,row){
        					if(row.dataSource.key == "GroupReserve"){
        						if(value.planItemGroup.allianceOrg){
        							return value.planItemGroup.allianceOrg.name;
        						}else{
        							return value.planItemGroup.servicePoint.name
        						}
        					}else{
    							return row.nonMemGroupReserve.nonMemberGroup.contactName;
        					}
        				}
        			},{
        				name:"destinationName",
        				label:"目的地",
        				format : function(value, row){
        					if(row.dataSource.key == "GroupReserve"){
        						return row.groupReserve.planItemGroup.planItem.destination.name;
        					}else{
        						return row.nonMemGroupReserve.nonMemberGroup.servicePoint == null ? "" : row.nonMemGroupReserve.nonMemberGroup.servicePoint.name;
        					}
        				}
        			},{
        				name:"groupReserve.checkInDate",
        				label:"入住日期",
        				format:function(value,row){
        					if(row.dataSource.key == "GroupReserve"){
        						return moment(value).format("YYYY-MM-DD");
        					}else{
        						return moment(row.nonMemGroupReserve.nonMemberGroup.checkInDate).format("YYYY-MM-DD");
        					}
        				}
        			},{
        				name:"groupReserve.checkOutDate",
        				label:"退住日期",
        				format:function(value,row){
        					if(row.dataSource.key == "GroupReserve"){
        						return moment(value).format("YYYY-MM-DD");
        					}else{
        						return moment(row.nonMemGroupReserve.nonMemberGroup.checkOutDate).format("YYYY-MM-DD");
        					}
        				}
        			},{
        				name:"signUpNum",
        				label:"报名人数",
        				format:function(value,row){
        					if(row.dataSource.key == "GroupReserve"){
        						return row.signUpNum;
        					}else{
        						if(row.nonMemGroupReserve.nonMemberGroup.signUpType && 
        								row.nonMemGroupReserve.nonMemberGroup.signUpType.key == "Experience"){
        							return row.signUpNum;
        						}else{
        							return row.nonMemGroupReserve.nonMemberGroup.perNumber;
        						}
        					}
        				}
        			},{
        				name:"roomStatus",
        				label:"房间配置",
        				format:function(value,row){
    						if(value){
    							return value.value;
    						}else{
    							return '';
    						}
        				}
        			},{
        				name:"checkInDinner",
        				label:"餐饮",
        				format:function(value,data){
        					if(!value || !value.status){
        						return "";
        					}else{
        						return value.status.value;
        					}
        				}
        			},{
        				name:"checkInCar",
        				label:"车辆",
        				format:function(value,data){
        					if(data.checkInCar.length == 0 && data.status.key=="Initial"){
        						return "";
        					}else if(data.checkInCar.length == 0){
        						return "无要求";
        					}else{
        						var checkInCar = data.checkInCar;
        						var flag = false;
        						for(var i=0;i < checkInCar.length;i++){
        							if(checkInCar[i].haveCar && checkInCar[i].status.key=="Pending"){
        								flag = true;
        							}
        						}
        						if(flag){
        							return "待落实";
        						}else{
        							return "已落实";
        						}
        					}
        				}
        			},{
        				name:"checkInMeetings",
        				label:"会议",
        				format:function(value,data){
        					if(data.checkInMeetings){
        						if(data.checkInMeetings.length == 0 && data.status.key=="Initial"){
        							return "";
        						}else if(data.checkInMeetings.length == 0){
        							return "无要求";
        						}else{
        							var checkInMeetings = data.checkInMeetings;
        							var flag = false;
        							for(var i=0;i < checkInMeetings.length;i++){
        								if(checkInMeetings[i].status.key == "Pending"){
        									flag = true;
        								}
        							}
        							if(flag){
        								return "待落实";
        							}else{
        								return "已落实";
        							}
        						}
        					}else{
        						return "";
        					}
        				}
        			},{
        				name:"status.value",
        				label:"状态"
        			},{
        				name:"operate",
        				label:"操作",
        				format:"button",
        				formatparams:[{
        					id:"set",
        					text:"制作",
        					show: function(value,data){
        						if(data.status.key == "Confirmed" || data.status.key == "Doing"){
        							return false;
        						}else{
        							return true;
        						}
        					},
        					handler:function(index,data,rowEle){
        						$("#checkInCar").html("")
        						ciiCarGrid.initComponent(widget);
        						widget.handler.grid.handleSetBtnClick(widget,data,false);
        					}
        				},{
        					id:"detail",
        					text:"详情",
        					show: function(value,data){
        						if(data.status.key == "Confirmed" || data.status.key == "Doing"){
        							return true;
        						}else{
        							return false;
        						}
        					},
        					handler:function(index,data,rowEle){

        						if(data.status.key=="Confirmed"){
        							$("#checkInCar").html("");
        							datilCarGrid.initComponent(widget);
        						}else{
        							$("#checkInCar").html("");
        							ciiCarGrid.initComponent(widget);
        							widget.get("cargrid").get("model").allowEdit=false;
        							widget.get("cargrid").get("model").allowAdd=false;
        						}
        						widget.handler.grid.handleSetBtnClick(widget,data,true);
        						widget.get("subnav").hide(["save"]);
        					}  
        				},{
    					  	id:"prepare",
    					  	text:"准备", 
    					  	show: function(value,data){
        						if(data.status.key == "Edited"){
        							return true;
        						}else{
        							return false;
        						}
        					},
    					  	handler:function(index,data,rowEle){
    					  		widget.handler.grid.handlePreBtnClick(widget,data);
    					  	}
        				}]
        			}]
        		}
        	});
        	widget.set("grid", grid);
        }
    };
    module.exports = instance;
});
